#include "threadPool.h"
#include "worker.h"
#include "path.h"
#include "puts_gets.h"
#include "password.h"
int createLogTrain(const char* info,train_t * ptrain){
   bzero(ptrain,sizeof(train_t));
    ptrain->flag = ISRESULT;
    ptrain->length = strlen(info);
    strcpy(ptrain->data,info);
}
int whetherLogin(int netfd,char* path){
    train_t train;
    cmd_t cmd;
    userinfo_t info;
    bzero(&train,sizeof(train_t));
    char loginStr[100]={0};
    strcpy(loginStr,"please input the username and the password\n");
    createLogTrain(loginStr,&train);
    while(1){
        bzero(&train,sizeof(train_t));
        int ret=recv(netfd,&train,sizeof(train_t),0);
        printf("ret=%d\n",ret);
        printf("%d\n",train.flag);
  
        memcpy(&info,train.data,sizeof(userinfo_t));
        int retLongin = passswordAuth(&info);
    //To do 接收用户验证函数返回值

        memcpy(&info,train.data,sizeof(info));
        printf("info=%s\n",info.username);

        bzero(path,256);
        sprintf(path,"/home/54th/netdisk/");

        if(retLongin ==0){
            //密码正确
            char  loginSucces[100] ={0};
            strcpy(loginSucces,"login successfull\n");
            


            send(netfd,&train,sizeof(train),0);
            strcat(path,info.username);
            strcat(path,"/");
            printf("path = %s",path);
            return 0;
    }
    else if(retLongin ==1){
        
        //用户不存在
        char noUser[100] = {0};
        strcpy(noUser,"wrong user,please input again");
        createLogTrain(noUser,&train);
        send(netfd,&train,sizeof(train),0);
    }
    else{
        //密码错误
         char loginFail[100] = {0};
         strcpy(loginFail,"password  wrong,please input again\n");
        createLogTrain(loginFail,&train);
        send(netfd,&train,sizeof(train),0);
    }
    
    }
}
